//
//  ASMakeSucceedController.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASMakeSucceedController.h"
#import "BDFCustomPhotoAlbum.h"

#define Image_Bili 1/1              //图片比例


@interface ASMakeSucceedController ()

@property (strong, nonatomic) UIImageView *mainImage;
@property (strong, nonatomic) UIView *bottomView;

@end

@implementation ASMakeSucceedController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.colorStr isNotBlank]) {
        self.view.backgroundColor = [UIColor colorWithHexString:self.colorStr];
    }else{
        self.view.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    }
    self.title = @"制作完成";
    [self createDoneBtn];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.mainImage];
    [self setAllViewLayout];
    [self saveImageToPhoto];
    // Do any additional setup after loading the view.
}

///保存图片到相册
- (void)saveImageToPhoto{
    ///开启相册权限
    NSString * title = title = [NSString stringWithFormat:@"%@(制作)",[ASTool appName]];
    [[BDFCustomPhotoAlbum shareInstance]saveToNewThumb:self.showImage ifPush:NO subTitle:title showHUD:YES completion:^(BOOL isSucceed, PHAuthorizationStatus photoType) {
        if (!isSucceed) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showPhotoAlertView:photoType];
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showSuccess:@"已保存到相册"];
            });
        }
    }];
}

- (void)showPhotoAlertView:(PHAuthorizationStatus)status{
    if (@available(iOS 14.0, *)) {
        if (status == PHAuthorizationStatusLimited) {
            return;
        }
    }
    NSString *contentStr = @"请在设置中为我们打开权限，以保存合成后的图片";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:contentStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alerOne = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
    UIAlertAction *alerTwo = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}];
    }];
    [alert addAction:alerOne];
    [alert addAction:alerTwo];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-(Height_SafeAreaBottom));
        make.height.mas_equalTo(150);
    }];
    
    CGFloat w = SCREEN_WIDTH - 30;
    CGFloat h = w/Image_Bili;
    if (h > SCREENH_HEIGHT - Height_NavBar - 32 - self.bottomView.top - 14) {
        h = SCREENH_HEIGHT - Height_NavBar - 32 - self.bottomView.top - 14;
    }
    [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(32 + Height_NavBar);
        make.height.mas_equalTo(h);
        make.width.mas_equalTo(w);
    }];
}

- (void)createDoneBtn{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 28)];
    [btn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor colorWithHexString:@"#FF7785"]];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 14;
    [btn addTarget:self action:@selector(clickDoneBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = bar;
}

- (void)clickDoneBtn{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (UIImageView *)mainImage{
    if (!_mainImage) {
        _mainImage = [[UIImageView alloc]init];
        _mainImage.contentMode = UIViewContentModeScaleAspectFit;
        [_mainImage setImage:self.showImage];
    }
    return _mainImage;
}

- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectZero];
        _bottomView.backgroundColor = self.view.backgroundColor;
        _bottomView.hidden = YES;
    }
    return _bottomView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
